package com.gonzapps.joydroidclient.main;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.gonzapps.joydroidclient.views.DualJoystickView;
import com.gonzapps.joydroidclient.views.JoystickMovedListener;

public class Main extends Activity {


	private static int ALPHA_RELEASED = 150;
	private static int ALPHA_PRESSED = 255;
	TextView txtX1, txtY1;
	TextView txtX2, txtY2;
	DualJoystickView joystick;
	ImageView triangle;
	ImageView circle;
	ImageView square;
	ImageView cross;
	ImageView dPadUp;
	ImageView dPadDown;
	ImageView dPadLeft;
	ImageView dPadRight;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);		
		initialize();
	}

	private void initialize(){
		
		txtX1 = (TextView)findViewById(R.id.TextViewX1);
		txtY1 = (TextView)findViewById(R.id.TextViewY1);

		txtX2 = (TextView)findViewById(R.id.TextViewX2);
		txtY2 = (TextView)findViewById(R.id.TextViewY2);

		joystick = (DualJoystickView)findViewById(R.id.dualjoystickView);

		joystick.setOnJostickMovedListener(_listenerLeft, _listenerRight);

		triangle = (ImageView)findViewById(R.id.triangle);

		triangle.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
					triangle.setAlpha(ALPHA_PRESSED);		            
					return true;
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					triangle.setAlpha(ALPHA_RELEASED);
					return false;		        	
				}
				return false;
			}
		});

		cross = (ImageView)findViewById(R.id.cross);

		cross.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
					cross.setAlpha(ALPHA_PRESSED);					
					return true;
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					cross.setAlpha(ALPHA_RELEASED);					
					return false;		        	
				}
				return false;
			}
		});

		square = (ImageView)findViewById(R.id.square);

		square.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
					square.setAlpha(ALPHA_PRESSED);		            
					return true;
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					square.setAlpha(ALPHA_RELEASED);
					return false;		        	
				}
				return false;
			}
		});
		
		circle = (ImageView)findViewById(R.id.circle);

		circle.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
					circle.setAlpha(ALPHA_PRESSED);		            
					return true;
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					circle.setAlpha(ALPHA_RELEASED);
					return false;		        	
				}
				return false;
			}
		});
		
		dPadUp = (ImageView)findViewById(R.id.dPadUp);

		dPadUp.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
					dPadUp.setImageResource(R.drawable.dpad_up_pressed);
					return true;
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					dPadUp.setImageResource(R.drawable.dpad_up);
					return false;		        	
				}
				return false;
			}
		});
		
		dPadDown = (ImageView)findViewById(R.id.dPadDown);

		dPadDown.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
					dPadDown.setImageResource(R.drawable.dpad_down_pressed);		            
					return true;
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					dPadDown.setImageResource(R.drawable.dpad_down);
					return false;		        	
				}
				return false;
			}
		});
		
		dPadLeft = (ImageView)findViewById(R.id.dPadLeft);

		dPadLeft.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
					dPadLeft.setImageResource(R.drawable.dpad_left_pressed);		            
					return true;
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					dPadLeft.setImageResource(R.drawable.dpad_left);
					return false;		        	
				}
				return false;
			}
		});
		
		dPadRight = (ImageView)findViewById(R.id.dPadRight);

		dPadRight.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
					dPadRight.setImageResource(R.drawable.dpad_right_pressed);		            
					return true;
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					dPadRight.setImageResource(R.drawable.dpad_right);
					return false;		        	
				}
				return false;
			}
		});
		
		triangle.setAlpha(ALPHA_RELEASED);
		cross.setAlpha(ALPHA_RELEASED);
		circle.setAlpha(ALPHA_RELEASED);
		square.setAlpha(ALPHA_RELEASED);
	}

	private JoystickMovedListener _listenerLeft = new JoystickMovedListener() {

		@Override
		public void OnMoved(int pan, int tilt) {
			txtX1.setText(Integer.toString(pan));
			txtY1.setText(Integer.toString(tilt));
		}

		@Override
		public void OnReleased() {
			txtX1.setText("released");
			txtY1.setText("released");
		}

		public void OnReturnedToCenter() {
			txtX1.setText("stopped");
			txtY1.setText("stopped");
		};
	}; 

	private JoystickMovedListener _listenerRight = new JoystickMovedListener() {

		@Override
		public void OnMoved(int pan, int tilt) {
			txtX2.setText(Integer.toString(pan));
			txtY2.setText(Integer.toString(tilt));
		}

		@Override
		public void OnReleased() {
			txtX2.setText("released");
			txtY2.setText("released");
		}

		public void OnReturnedToCenter() {
			txtX2.setText("stopped");
			txtY2.setText("stopped");
		};
	}; 
}
